package com.tim.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestEchoClient {

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8888);
		Socket client = new Socket();
		client.connect(addr, 1000 * 10);

		client.setTcpNoDelay(true);// 数据不做缓冲，立即发送
		client.setSoLinger(true, 0);// socket关闭时，立即释放资源
		client.setKeepAlive(true);
		client.setTrafficClass(0x04 | 0x10);// 高可靠性和最小延时传输

		BufferedReader buf = null;
		PrintStream out = null;
		BufferedReader input = null;
		input = new BufferedReader(new InputStreamReader(System.in));
		buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
		out = new PrintStream(client.getOutputStream());
		boolean flag = true;
		while (flag) {
			System.out.println("请输入信息");
			String str = input.readLine();
			out.println(str);
			if ("bye".equals(str)) {
				flag = false;
			} else {
				String echo = buf.readLine();
				System.out.println(echo);
			}

		}
		buf.close();
		client.close();
	}

}
